package com.ebt.m.proposal_v2.widget.base;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.app.FragmentManager;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.ebt.m.proposal_v2.helper.ThemeHelper;
import com.ebt.m.proposal_v2.utils.ViewUtils;

import butterknife.ButterKnife;

/**
 * 继承LinearLayout的基类
 *
 * @author archer.qi
 *         Created on 2017/5/5.
 */
public abstract class CompatLinearLayout extends LinearLayout {
    private Context mContext;
    protected View contentView;
    private FragmentManager fragmentManager;

    public CompatLinearLayout(Context context) {
        this(context, null);
    }

    public CompatLinearLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CompatLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
    }

    private void init() {
        int viewId = setContentView();
        contentView = ViewUtils.inflate(mContext, viewId);
        if (contentView != null) {
            LinearLayout.LayoutParams params = new LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            addView(contentView, params);
            ButterKnife.bind(this, contentView);
            onBindView();
        }
    }

    protected abstract int setContentView();

    protected abstract void onBindView();

    protected View find(int resId) {
        if (contentView == null) {
            throw new NullPointerException("no view added, please return valid resourceId by setContentView().");
        }
        return contentView.findViewById(resId);
    }

    protected Drawable getDrawable(int resId) {
        return ThemeHelper.getDrawable(mContext, resId);
    }

    protected int getColor(int resId) {
        return ThemeHelper.getColor(mContext, resId);
    }

    protected float getDimen(int resId) {
        return ThemeHelper.getDimen(mContext, resId);
    }

    protected String getString(int resId) {
        return ThemeHelper.getString(mContext, resId);
    }

    protected Context getViewContext() {
        return mContext;
    }

    protected View getContentView() {
        return contentView;
    }

    protected String checkNull(String str) {
        return str == null ? "" : str;
    }

    public void setFragmentManager(FragmentManager manager) {
        this.fragmentManager = manager;
    }

    public FragmentManager getFragmentManager() {
        return this.fragmentManager;
    }
}
